package net.jxtz.jxtzos.authentication;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.authentication.exception.InvalidJwtTokenException;
import net.jxtz.jxtzos.authentication.util.MyResponseException;
import net.jxtz.jxtzos.entity.web.R;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.AuthorizationServiceException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author a123
 * 权限校验失败
 */
@Slf4j
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {

        log.info("进入了accessDenied");

        if (accessDeniedException instanceof AuthorizationServiceException){
            log.error("资源校验异常，权限不足");
            String s = JSONObject.toJSONString(R.error().code(40003).message("资源校验异常，权限不足"));
            MyResponseException.responseEntity(response, s);
        }

        if (accessDeniedException instanceof InvalidJwtTokenException){
            String s = JSONObject.toJSONString(R.error().code(40004).message("身份过期，请重新登陆"));
            MyResponseException.responseEntity(response, s);
        }

    }


}
